package cn.lxh.Interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @author lxh Create Time: 2023/12/27 0:52
 * Feign拦截器
 */
@Slf4j
public class SaTokenInterceptor implements RequestInterceptor {

    @Value("${sa-token.token-name}")
    private String tokenName;
    @Override
    public void apply(RequestTemplate requestTemplate) { // RequestTemplate请求模版 (封装好的请求参数, 请求url), 代表当前feign的方法
        // 获取请求对象
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        // 获取请求头中的token
        String satoken = requestAttributes.getRequest().getHeader("satoken");
        log.info("请求头中的token转发====={}", satoken);
        // 给feign接口发起的请求设置请求头
        requestTemplate.header(tokenName, satoken);
    }
}
